﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using CRMWeb.Utilities.Context;
using Newtonsoft.Json;


namespace CRMWeb.WebServices.AM
{
    /// <summary>
    /// Summary description for Ledger
    /// </summary>
    public class Ledger : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();
            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        public void LedgerSelect()
        {
            string strJson = null;
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                int LoanID = string.IsNullOrEmpty(context.Request.QueryString["LoanID"]) ? 0 : int.Parse(context.Request.QueryString["LoanID"]);
                DataSet ds = LedgerHandler.GetLedgerList(pageIndex, pageSize, LoanID);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);                 
            }
        }

        public void AddLedger(string LedgerDate, string TransactionsCharge, string Repayment1, string DebitAmount, string CreditAmount, string ClosingBalance, string LoanID)
        {
            try
            {
                int RepaymentID = LedgerHandler.AddLedger(LedgerDate, TransactionsCharge, Repayment1, DebitAmount, CreditAmount, ClosingBalance, LoanID);
                context.Response.Write(RepaymentID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public int LedgerEdit(int ledgerid, string LedgerDate, string TransactionsCharge, string Repayment1, string DebitAmount, string CreditAmount, string ClosingBalance)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = LedgerHandler.EditLedger(ledgerid, LedgerDate, TransactionsCharge, Repayment1, DebitAmount, CreditAmount, ClosingBalance);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public bool LedgerDelete(int ledgerid)
        {
            bool flag = false;
            try
            {
                flag = LedgerHandler.DeleteLedger(ledgerid);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        public void getClosingBalance(int LoanID)
        {
            try
            {
                DataTable dt = LedgerHandler.getClosingBalance(LoanID);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            } 
        }

        public void GenerateLadger(int LoanID)
        {
            try
            {
                if (LoanID != 0)
                {
                    DataTable dt = LedgerHandler.GetLedgerPrint(LoanID);
                    string Print = "<table title='Ledger'><tr><th width='200'>Date</th>";
                    Print += "<th width='200'>Transactions Charge</th>";
                    Print += "<th width='200'>Repayment</th>";
                    Print += "<th width='200'>Closing Balance</th></tr>";
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            Print += "<tr><td>" + Convert.ToString(dt.Rows[i]["date"]) + "</td>";
                            Print += "<td>" + Convert.ToString(dt.Rows[i]["description"]) + "</td>";
                            Print += "<td>" + Convert.ToString(dt.Rows[i]["amount"]) + "</td>";
                            Print += "<td>" + Convert.ToString(dt.Rows[i]["balance"]) + "</td>";
                        }
                        Print += "</table>";
                        context.Response.Write(Print);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
    }
}